package com.xzy.cinematicket.dao.impl;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import com.xzy.cinematicket.dao.IUserDao;
import com.xzy.cinematicket.entity.User;
import com.xzy.cinematicket.util.DbUtil;

/**
 * @author Sun
 */
public class UserDaoImpl implements IUserDao{
private QueryRunner qr = new QueryRunner(DbUtil.getDataSource());
	
	@Override
	public int insert(User user) {
		int flag = 0;
		try {
			Object params[] = {user.getUsername(),user.getPassword(),user.getPhone(),user.getFlag()};
			String sql = "insert into user value(null,?,?,?,?)";
			flag = qr.update(sql ,params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}

	
	@Override
	public boolean selectByname(String username) {
		boolean flag = false;
		User u = null;
		try {
			String sql = " select * from user where username = ? ";
			u = qr.query(sql, new BeanHandler<User>(User.class),username);
			if(u != null)
				flag = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}

	
	@Override
	public User select(User user) {
		User u = null;
		try {
			String sql = " select * from user where username = ? and password = ?";
			Object params[] = {user.getUsername(),user.getPassword()};
			u = qr.query(sql, new BeanHandler<User>(User.class),params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return u;
	}


	@Override
	public int deleteByName(String username) {
		int flag = 0;
		try {
			String sql = " delete from user where username = ? ";
			flag = qr.update(sql ,username);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}
	
}
